//
//  MenuView.m
//  ALFashion
//
//  Created by dengzhicheng on 15/6/13.
//  Copyright (c) 2015年 dengzhicheng. All rights reserved.
//

#import "MenuView.h"
#import "MenuTableViewCell.h"

@interface MenuView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) MenuTableViewCell *cell;

@end

@implementation MenuView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setBackgroundColor:[UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.95]];
        [self setSubViews];
    }
    return self;
}
- (void)setSubViews
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    [self.table setBackgroundColor:[UIColor clearColor]];
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.table.rowHeight = 55;
    self.table.delegate = self;
    self.table.dataSource = self;
    [self addSubview:self.table];
    [self createView];


}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 11;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!self.cell) {
        self.cell = [[MenuTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

    }
    
    // cell 动画
    if (indexPath.row % 2) {
        
        self.cell.frame = CGRectMake(-320, self.cell.frame.origin.y, self.cell.frame.size.width, self.cell.frame.size.height);
        
        [UIView animateWithDuration:0.7 animations:^{
            self.cell.frame = CGRectMake(0, self.cell.frame.origin.y, self.cell.frame.size.width, self.cell.frame.size.height);
        } completion:^(BOOL finished) {
            ;
        }];
    }
    else{
        
        self.cell.frame = CGRectMake(320 ,self.cell.frame.origin.y ,self.cell.frame.size.width, self.cell.frame.size.height);
        
        [UIView animateWithDuration:0.7 animations:^{
            self.cell.frame = CGRectMake(0, self.cell.frame.origin.y, self.cell.frame.size.width, self.cell.frame.size.height);
        } completion:^(BOOL finished) {
            ;
        }];
        
    }
    
    if (indexPath.row == 0) {
        [self.cell.view removeFromSuperview];
    }
    
    if (indexPath.row == 1) {
        self.cell.classifyLabel_C.text = @"首页";
        self.cell.classifyLabel_E.text = @"HOME";
        
    } else if (indexPath.row == 2) {
        self.cell.classifyLabel_C.text = @"穿搭";
        self.cell.classifyLabel_E.text = @"STYLE";

    } else if (indexPath.row == 3) {
        self.cell.classifyLabel_C.text = @"配饰";
        self.cell.classifyLabel_E.text = @"ACCESSORIES";

    } else if (indexPath.row == 4) {
        self.cell.classifyLabel_C.text = @"美妆";
        self.cell.classifyLabel_E.text = @"BEAUTY";

    } else if (indexPath.row == 5) {
        self.cell.classifyLabel_C.text = @"生活";
        self.cell.classifyLabel_E.text = @"LIFESTYLE";

    } else if (indexPath.row == 6) {
        self.cell.classifyLabel_C.text = @"明星";
        self.cell.classifyLabel_E.text = @"CELEBRITY";

    } else if (indexPath.row == 7) {
        [self.cell.view removeFromSuperview];
        
    } else if (indexPath.row == 8) {
        self.cell.classifyLabel_C.text = @"我的";
        self.cell.classifyLabel_E.text = @"PROFILE";

    } else if (indexPath.row == 9) {
        self.cell.classifyLabel_C.text = @"收藏";
        self.cell.classifyLabel_E.text = @"FAVOURITE";

    } else if (indexPath.row == 10) {
        self.cell.classifyLabel_C.text = @"设置";
        self.cell.classifyLabel_E.text = @"SETTING";

    }
    
    [self.cell setBackgroundColor:[UIColor clearColor]];
    [self.cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return self.cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"indexPath.row = %ld", indexPath.row);
    // 收回菜单
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MenuView" object:@(NO)];
    
    UITabBarController *tabBar = (UITabBarController *)self.window.rootViewController;
    
    switch (indexPath.row) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
            tabBar.selectedViewController = tabBar.viewControllers[indexPath.row - 1];
            break;
        case 8:
        case 9:
        case 10:
            [self.delegate presentVC:indexPath.row];
            break;
    }
    
}


- (void)createView
{
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 30, 20, 20)];
    [backButton setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backButton];
    
}

- (void)backButtonClick:(UIButton *)button
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MenuView" object:@(NO)];
}


@end
